import { useReducer } from 'react';

export interface IResourRangeState {
  searchValue: string;
  selectKeys: string[];
  expandedKeys: string[];
  expandedRowKeys: string[];
  autoExpandParent: boolean;
  searchTableValue: string;
  checkedRowKeys: string[];
  checkTableRows: Record<string, any>;
}

interface IActionType {
  type:
    | 'range-searchValue'
    | 'range-selectKeys'
    | 'range-expandedKeys'
    | 'range-autoExpandParent'
    | 'remove-all-state'
    | 'range-searchTableValue'
    | 'range-expandedRowKeys'
    | 'range-checkedRowKeys'
    | 'range-checkTableRows';
  payload: any;
}

export const useResourRangeReducer = () => {
  const initState: IResourRangeState = {
    searchValue: '',
    selectKeys: [],
    expandedKeys: [],
    expandedRowKeys: [],
    autoExpandParent: false,
    searchTableValue: '',
    checkedRowKeys: [],
    checkTableRows: {},
  };

  const reducer = (state: IResourRangeState, actions: IActionType) => {
    switch (actions?.type) {
      case 'range-searchValue': //左侧树 搜索值
        return {
          ...state,
          searchValue: actions?.payload,
        };
      case 'range-selectKeys': //左侧树 选中值
        return {
          ...state,
          selectKeys: actions?.payload,
        };
      case 'range-expandedKeys': //左侧树 选中值展开父节点
        return {
          ...state,
          expandedKeys: actions?.payload,
        };
      case 'range-autoExpandParent': //左侧树 是否自动展开
        return {
          ...state,
          autoExpandParent: actions?.payload,
        };
      case 'range-searchTableValue': //右侧表格 搜索值
        return {
          ...state,
          searchTableValue: actions?.payload,
        };
      case 'range-expandedRowKeys': //右侧表格选中项父级 展开 值
        return {
          ...state,
          expandedRowKeys: actions?.payload,
        };
      case 'range-checkedRowKeys': //表格选中项key
        return {
          ...state,
          checkedRowKeys: actions?.payload,
        };
      case 'range-checkTableRows': //表格项点击选中的值
        return {
          ...state,
          checkTableRows: actions?.payload,
        };
      case 'remove-all-state': {
        return {
          ...state,
          ...initState,
        };
      }
      default:
        return {
          ...state,
        };
    }
  };

  const [state, resRangeDispatch] = useReducer(reducer, initState);
  return {
    ...state,
    resRangeDispatch,
  };
};
